import java.util.ArrayList;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liferay.portal.kernel.util.ServerDetector;

public class ServerInfoUtil
{
  private static final Logger log = LoggerFactory.getLogger(ServerInfoUtil.class);
  
  public static Integer getServerPort(String protocol, String scheme)
    throws Exception
  {
    MBeanServer mBeanServer = null;
    ArrayList<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    int serverType = 1;
    if (mBeanServers.size() > 0) {
      for (MBeanServer _mBeanServer : mBeanServers)
      {
        mBeanServer = _mBeanServer;
        if (ServerDetector.isTomcat())
        {
          log.info("��������������������tomcat");
          serverType = 1;
        }
        if (ServerDetector.isJBoss())
        {
          log.info("��������������������jboss");
          serverType = 2;
        }
        if (ServerDetector.isWebLogic())
        {
          log.info("��������������������weblogic");
          serverType = 3;
        }
      }
    }
    if (mBeanServer == null) {
      throw new IllegalStateException("��������JVM��������MBeanServer.");
    }
    Set<ObjectName> objectNames = null;
    switch (serverType)
    {
    case 1: 
      objectNames = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
      break;
    case 2: 
      objectNames = mBeanServer.queryNames(new ObjectName("jboss.web:type=Connector,*"), null);
      break;
    case 3: 
      return getPortInWeblogic();
    default: 
      throw new IllegalStateException("��������������������");
    }
    if ((objectNames == null) || (objectNames.size() <= 0)) {
      throw new IllegalStateException("��������JVM��������MBeanServer : " + mBeanServer.getDefaultDomain() + " ������������.");
    }
    for (ObjectName objectName : objectNames)
    {
      Object _protocol = mBeanServer.getAttribute(objectName, "protocol");
      Object _scheme = mBeanServer.getAttribute(objectName, "scheme");
      if ((protocol.equals(_protocol)) && (scheme.equals(_scheme))) {
        return (Integer)mBeanServer.getAttribute(objectName, "port");
      }
    }
    return null;
  }
  
  private static Integer getPortInWeblogic()
    throws Exception
  {
    ObjectName service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
    InitialContext ctx = new InitialContext();
    MBeanServer mBeanServer = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");
    ObjectName objectName = (ObjectName)mBeanServer.getAttribute(service, "ServerRuntime");
    if (objectName == null) {
      throw new IllegalStateException("��������JVM��������MBeanServer : " + mBeanServer.getDefaultDomain() + " ������������.");
    }
    return (Integer)mBeanServer.getAttribute(objectName, "ListenPort");
  }
}

